﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace IBS
{
    /// <summary>
    /// Подключение к базе IBS
    /// </summary>
    public partial class IbsDataContext
    {
        /// <summary>
        /// Отправка сообщения
        /// </summary>
        /// <param name="writeOff"></param>
        public void SendMail(string sender, string recipient, string subject, string message)
        {
            recipient.Split(new char[] { ';', ',', ' ' })
                .ToList()
                .ForEach(r =>
                {
                    if (r.Length != 0)
                        this.SendMailItem(sender, r, subject, message);
                });
        }

        private void SendMailItem(string sender, string recipient, string subject, string message)
        {
            using (var connection = new OracleConnection(this.connectionString))
            {
                connection.Open();
                using (var command = new OracleCommand())
                {
                    command.Connection = connection;
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "IBSDBO.SendMail";
                    command.Parameters.Clear();
                    command.Parameters.AddWithValue("pSender", sender);
                    command.Parameters.AddWithValue("pRecipient", recipient);
                    command.Parameters.AddWithValue("pSubject", subject);
                    command.Parameters.AddWithValue("pMessage", message);
                    command.ExecuteNonQuery();
                }
            }
        }
    }
}